package com.seeyon.apps.ext.kingdee.fieldCtrl;


import com.seeyon.cap4.form.bean.FormAuthViewFieldBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormFieldBean;

import com.seeyon.cap4.form.bean.fieldCtrl.FormFieldCustomCtrl;
import com.seeyon.cap4.form.util.Enums;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.filemanager.manager.AttachmentManager;
import com.seeyon.ctp.util.StringUtil;
import com.seeyon.ctp.util.UUIDLong;
import com.seeyon.ctp.util.json.JSONUtil;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DocUploadFormCtrl1  extends FormFieldCustomCtrl {

    private AttachmentManager attachmentManager;

    public DocUploadFormCtrl1() {
    }


    public void setAttachmentManager(AttachmentManager attachmentManager) {
        this.attachmentManager = attachmentManager;
    }

    @Override
    public String getKey() {
        return "5484916566399136200";
    }

    @Override
    public String getFieldLength() {
        return "5000";
    }

    @Override
    public String getText() {
        return "下载附件";
    }

    @Override
    public void init() {
        this.setPluginId("doc00");
        this.setIcon("cap-icon-e-invoice");

    }

    @Override
    public String[] getDefaultVal(String s) {
        return new String[0];
    }

    @Override
    public String getPCInjectionInfo() {
        Map<String, String> injectInfo = new HashMap<String, String>();
        injectInfo.put("path", "apps_res/cap/customCtrlResources/docFormCtrlResources1/");
        injectInfo.put("jsUri", "js/docUploadFormControls.js");
        injectInfo.put("initMethod", "init");
        injectInfo.put("nameSpace", "field_" + this.getKey());
        return JSONUtil.toJSONString(injectInfo);
    }

    @Override
    public String getMBInjectionInfo() {
        Map<String, String> injectInfo = new HashMap<String, String>();
        injectInfo.put("path", "http://doc.v5.cmp/v1.0.0/");
        injectInfo.put("weixinpath", "doc");
        injectInfo.put("jsUri", "js/customCtrlResources1/appContentFormCtrl.js");
        injectInfo.put("initMethod", "init");
        injectInfo.put("nameSpace", "field_" + this.getKey());
        return JSONUtil.toJSONString(injectInfo);
    }

    @Override
    public boolean isAttachment() {
        return true;
    }
    @Override
    public boolean canInSubTable() {
        return false;
    }

    @Override
    public boolean canUse() {
        return AppContext.hasPlugin("doc00") || AppContext.hasPlugin("cap4");
    }

    @Override
    public boolean canInWorkFlowBranch() {
        return false;
    }

    @Override
    public boolean canBathUpdate() {
        return false;
    }

    @Override
    public boolean canInMeetingTrigger() {
        return false;
    }


    public boolean canInTriggerFillBack() {
        return true;
    }

    @Override
    public Enums.FieldType[] getFieldType() {
        return new Enums.FieldType[]{Enums.FieldType.LONGTEXT};
    }

    @Override
    public List<String[]> getListShowDefaultVal(Integer integer) {
        return null;
    }

    @Override
    public Object genVal(Object oldVal) {
        return StringUtil.checkNull(String.valueOf(oldVal)) ? UUIDLong.longUUID() : oldVal;
    }

    public boolean authNotNullAndValIsNull(FormDataMasterBean formDataMasterBean, FormFieldBean field, FormAuthViewFieldBean authViewFieldBean, Object val) {
        String strVal = String.valueOf(val);
        boolean result = false;
        if (authViewFieldBean.isNotNullable()) {
            if (StringUtil.checkNull(strVal)) {
                result = true;

            } else if (formDataMasterBean.getSessionAttachments(strVal)!=null) {
                result = CollectionUtils.isEmpty(formDataMasterBean.getSessionAttachments(strVal));
            } else {
                result = CollectionUtils.isEmpty(this.attachmentManager.getBySubReference(Long.parseLong(strVal)));
            }
        }

        return result;
    }

}
